﻿using System;
using System.Data;

namespace VitCode.Castle.Helper
{
    /// <summary>
    /// RepeatColumnChecker 的摘要说明。
    /// </summary>
    public class RepeatColumnChecker : EmptyColumnChecker
    {
        private DataColumnValues columnValues = new DataColumnValues();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="excelHelper"></param>
        public RepeatColumnChecker(DataTableCheckerHelper excelHelper) : base(excelHelper)
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        private static string ConvertToString(int[] s)
        {
            string result = null;
            for (int i = 0; i < s.Length; i++)
            {
                if (i == 0)
                    result = Convert.ToString(s[i]).Trim();
                else
                    result += "," + Convert.ToString(s[i]);
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="columnIndex"></param>
        /// <param name="row"></param>
        /// <param name="rowIndex"></param>
        /// <param name="errorResult"></param>
        protected override void EachCheck(ColumnIndex columnIndex, DataRow row, int rowIndex, ErrorResult errorResult)
        {
            base.EachCheck(columnIndex, row, rowIndex, errorResult);
            string dataColumnIndexes = ConvertToString(columnIndex.GetColumnIndexes(row.Table.Columns));
            string rowValue = columnIndex.GetValues(row);
            bool exist = columnValues.Contains(dataColumnIndexes, rowValue, rowIndex);
            string captions = columnIndex.GetCaptionNames(row);
            if (exist)
            {
                int CrushRowIndex = columnValues.GetRepeatRowNumber(rowValue, dataColumnIndexes);
                errorResult.SetMessage(DataTableCheckerHelper.GetSourceDataIndex(rowIndex), "<font color=red>列名为" + captions + "与行号为" + this.DataTableCheckerHelper.GetSourceDataIndex(CrushRowIndex) + "的数据重复</font>");

            }
        }

    }
}
